package com.jastar.android.test.base.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ImagePagerAdapter extends PagerAdapter {
    private List<ImageView> viewList = new ArrayList<>();

    public ImagePagerAdapter(Context context, int[] images) {
        Arrays.stream(images).forEach(image -> {
            ImageView item = new ImageView(context);
            item.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            item.setImageResource(image);
            viewList.add(item);
        });
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //实例化指定位置的页面，并添加到容器，返回一个跟这个页面可以关联的对象
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView item = viewList.get(position);
        container.addView(item);
        return item;
    }

    //从容器销毁指定位置的页面
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(viewList.get(position));
    }

}
